package com.canin.member.action;

import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;

import org.apache.struts2.interceptor.SessionAware;

import com.canin.Constant;
import com.canin.dao.DAOFactory;
import com.canin.dao.DogDAO;
import com.canin.model.Dog;
import com.canin.model.Member;
import com.canin.util.CaninUtil;
import com.opensymphony.xwork2.ActionSupport;

public class DogAction extends ActionSupport implements SessionAware
{
	private static final long serialVersionUID = 1486003668139713989L;
	
	private Map<String, Object> userSession;
	
	private Dog dogForm = new Dog();
	private Dog dog = new Dog();
	private Member member = new Member();

	private Map<String, String> breedNameList = null;
	private Map<String, String> genders = null;
	private Map<String, String> months = null;
	private Map<String, String> years = null;
	private Map<String, String> coats = null;
	private Map<String, String> maneFlag = null;
	private Map<String, String> pregnantWeekList = null;
	private Map<String, String> lactateWeekList = null;
	private Map<String, String> activityLevelList = null;
	private Map<String, String> allergyToBeefFlag = null;
	private Map<String, String> allergyToPorkFlag = null;
	private Map<String, String> allergyToChickenFlag = null;
	private Map<String, String> allergyToFishFlag = null;
	private Map<String, String> allergyToMilkFlag = null;
	private Map<String, String> allergyToWheyFlag = null;
	private Map<String, String> allergyToEggFlag = null;
	private Map<String, String> allergyToSoybeanFlag = null;
	private Map<String, String> allergyToCornFlag = null;
	
	private Map<String, String> dogNameList = null;
	
	public String toAdd() throws Exception
	{
		initialValue();
		return "toAdd";
	}
	
	public String add() throws Exception
	{
		initialValue();
		member = (Member)userSession.get( Constant.MEMBER );
		
		if ( dogForm != null )
		{
			// Required Fields
			if ( !"-1".equals(dogForm.getBreedNameList())
				&& !"".equals(dogForm.getDogName())
				&& !"-1".equals(dogForm.getGenders())
				&& !"-1".equals(dogForm.getYears())
				&& !"-1".equals(dogForm.getMonths()) )
			{
				DAOFactory daoFactory = new DAOFactory();
				daoFactory.beginConnection();

				DogDAO dogDAO = daoFactory.createDogDAO();
				// Dog Static
				dogDAO.createNewDogStatic( member.getMemberId(), dogForm );
				Dog dog = dogDAO.getDogStaticbyName( member.getMemberId(), dogForm.getDogName() );
				
				// Dog Dynamic
				dogForm.setDogStaticId(dog.getDogStaticId());
				dogDAO.createNewDogDynamic( dogForm );
				
				addActionMessage("เพิ่มข้อมูล " + dog.getDogName() + " เรียบร้อย");
			}
			else
			{
				addActionMessage("กรุณากรอกข้อมูลให้ครบถ้วน");
			}
		}
		
		return "add";
	}
	
	public String toEdit() throws Exception
	{
		initialValue();
		member = (Member)userSession.get( Constant.MEMBER );
		setDogNameList( CaninUtil.retrieveDogNameList( member.getMemberId() ) );
		
		return "toEdit";
	}
	
	public String search() throws Exception
	{
		initialValue();
		member = (Member)userSession.get( Constant.MEMBER );
		setDogNameList( CaninUtil.retrieveDogNameList( member.getMemberId() ) );
		
		// search for breed --> add to breedForm
		DAOFactory daoFactory = new DAOFactory();
		daoFactory.beginConnection();
		DogDAO dogDAO = daoFactory.createDogDAO();
		
		if ( !"-1".equals(dogForm.getDogNameList() ) )
		{
			// Only for Display
			dog = dogDAO.getDogStaticById( member.getMemberId(), Integer.parseInt(dogForm.getDogNameList()) );
			dogForm.setAge( CaninUtil.calAge( dog.getYearOfBirth(), dog.getMonthOfBirth() ) );
			dogForm.setBreedName( CaninUtil.getBreedNameByBreedId( dog.getBreedId() ) );
			dogForm.setSexDisplay( genders.get( dog.getSex() ) );
			
			// Get Dog Dynamic
			dog = dogDAO.getDogDynamicById( dog );
			dogForm.setLifeSpan( CaninUtil.calLifeSpan( dog.getYearOfBirth(),
					dog.getMonthOfBirth(), dog.getPregnantWeeks(), 
					dog.getLactateWeeks(), dog.getBreedId() ) );
		
			// Always update follow the Dog Growth
			dog = dogDAO.getDogDynamicById( dog );
			
//			System.out.println("\n\n\n\n");
//			System.out.println("act level :" + dog.getActivityLevel() + "\nmane:" + dog.getDogMane());
//			System.out.println("\n\n\n\n");
			
			dogForm.setDateCount( CaninUtil.generateDateCount( dog.getUpdatedDate() ) );
//			dogForm.setDateCount( CaninUtil.testSString() );
			dogForm.setDogWeight(dog.getDogWeight());
			dogForm.setDogCoat( dog.getDogCoat() );
			dogForm.setDogMane( dog.getDogMane() );
			dogForm.setPregnantWeeks( dog.getPregnantWeeks() );
			dogForm.setLactateWeeks( dog.getLactateWeeks() );
			dogForm.setActivityLevel( dog.getActivityLevel() );
			dogForm.setAllergyToBeef( dog.getAllergyToBeef() );
			dogForm.setAllergyToPork( dog.getAllergyToPork() );
			dogForm.setAllergyToChicken( dog.getAllergyToChicken() );
			dogForm.setAllergyToFish( dog.getAllergyToFish() );
			dogForm.setAllergyToMilk( dog.getAllergyToMilk() );
			dogForm.setAllergyToWhey( dog.getAllergyToWhey() );
			dogForm.setAllergyToEgg( dog.getAllergyToEgg() );
			dogForm.setAllergyToSoybean( dog.getAllergyToSoybean() );
			dogForm.setAllergyToCorn( dog.getAllergyToCorn() );
			
			userSession.put(Constant.DOG, dog);
		}
		else
		{
//			addActionMessage("กรุณา");
		}
		
		return "search";
	}
	
	public String edit() throws Exception
	{
		initialValue();
		boolean valid = false;
		
		member = (Member)userSession.get( Constant.MEMBER );
		setDogNameList( CaninUtil.retrieveDogNameList( member.getMemberId() ) );
		
		dog = (Dog)userSession.get( Constant.DOG );
		
		// Check if dog is male --> can not pragnanat/lactate
		System.out.println("\n\n\n *-*-*--*-*--* "
		+ dog.getSex() + ":" + dogForm.getPregnantWeekList()
		+ ":" + dogForm.getLactateWeekList() + "\n\n\n\n");
		
		if (  "F".equals(dog.getSex() ) )
		{
			valid = true;
		}
		else if ( ( "-1".equals(dogForm.getPregnantWeekList()) || dogForm.getPregnantWeekList() == null ) 
			&& ( "-1".equals(dogForm.getLactateWeekList()) || dogForm.getLactateWeekList() == null ) )
		{
			valid = true;
		}
		else
		{
			valid = false;
		}
		
		if ( valid )
		{
//			System.out.println("\n\n\n1 " + dog.getDogDynamicId() + "\n\n\n\n");
			
			dogForm.setDogStaticId( dog.getDogStaticId() );
		
			DAOFactory daoFactory = new DAOFactory();
			daoFactory.beginConnection();
			DogDAO dogDAO = daoFactory.createDogDAO();
			
			dogDAO.createNewDogDynamic(dogForm);
			
			dog = dogDAO.getDogDynamicById(dogForm);
			
	//		System.out.println("\n\n\n2 " + dog.getDogDynamicId() + "\n\n\n\n");
			
			dogForm.setDogWeight(dog.getDogWeight());
			dogForm.setDogCoat( dog.getDogCoat() );
			dogForm.setDogMane( dog.getDogMane() );
			dogForm.setPregnantWeeks( dog.getPregnantWeeks() );
			dogForm.setLactateWeeks( dog.getLactateWeeks() );
			dogForm.setActivityLevel( dog.getActivityLevel() );
			dogForm.setAllergyToBeef( dog.getAllergyToBeef() );
			dogForm.setAllergyToPork( dog.getAllergyToPork() );
			dogForm.setAllergyToChicken( dog.getAllergyToChicken() );
			dogForm.setAllergyToFish( dog.getAllergyToFish() );
			dogForm.setAllergyToMilk( dog.getAllergyToMilk() );
			dogForm.setAllergyToWhey( dog.getAllergyToWhey() );
			dogForm.setAllergyToEgg( dog.getAllergyToEgg() );
			dogForm.setAllergyToSoybean( dog.getAllergyToSoybean() );
			dogForm.setAllergyToCorn( dog.getAllergyToCorn() );
			
			addActionMessage("แก้ไขข้อมูลเรียบร้อย");
		}
		else
		{
			addActionMessage("สุนัขตัวผู้ ไม่สามารถถตั้งท้องหรือให้นมลูกได้ \n กรุณากรอกข้อมูลใหม่อีกครั้ง");
		}
		
		return "edit";
	}
	
	private void initialValue() throws Exception
	{
		setBreedNameList( CaninUtil.retrieveBreedNameList() );
		setGenders(initializeGenders());
		setMonths(initializeMonths());
		setYears(initializeYears());
		setCoats(initializeCoats());
		setManeFlag(initializeFlag());
		setPregnantWeekList(initializeWeek(9));
		setLactateWeekList(initializeWeek(4));
		setActivityLevelList(initializeActivityLevel());
		setAllergyToBeefFlag(initializeFlag());
		setAllergyToPorkFlag(initializeFlag());
		setAllergyToChickenFlag(initializeFlag());
		setAllergyToFishFlag(initializeFlag());
		setAllergyToMilkFlag(initializeFlag());
		setAllergyToWheyFlag(initializeFlag());
		setAllergyToEggFlag(initializeFlag());
		setAllergyToSoybeanFlag(initializeFlag());
		setAllergyToCornFlag(initializeFlag());
	}
	
	private Map<String, String> initializeGenders()
	{
		TreeMap<String, String> genders = new TreeMap<>();
		genders.put(Dog.MALE, getText("ผู้"));
		genders.put(Dog.FEMALE, getText("เมีย"));
		
		return genders;
	}
	
	private Map<String, String> initializeMonths()
	{
		TreeMap<String, String> months = new TreeMap<>();
		months.put(Dog.MONTH_1, getText("1 - มกราคม"));
		months.put(Dog.MONTH_2, getText("2 - กุมภาพันธ์"));
		months.put(Dog.MONTH_3, getText("3 - มีนาคม"));
		months.put(Dog.MONTH_4, getText("4 - เมษายน"));
		months.put(Dog.MONTH_5, getText("5 - พฤษภาคม"));
		months.put(Dog.MONTH_6, getText("6 - มิถุนายน"));
		months.put(Dog.MONTH_7, getText("7 - กรกฎาคม"));
		months.put(Dog.MONTH_8, getText("8 - สิงหาคม"));
		months.put(Dog.MONTH_9, getText("9 - กันยายน"));
		months.put(Dog.MONTH_10, getText("10 - ตุลาคม"));
		months.put(Dog.MONTH_11, getText("11 - พฤศจิกายน"));
		months.put(Dog.MONTH_12, getText("12 - ธันวาคม"));
		
		return months;
	}
	
	private Map<String, String> initializeYears()
	{
		TreeMap<String, String> years = new TreeMap<>();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int thaiYear = year + 543;
		
		for ( int i = 0; i < 30; i++ )
		{
			years.put(Integer.toString(thaiYear - i), Integer.toString(thaiYear - i));
		}
		
		return years;
	}
	
	/*
	1= ขนสั้นและเรียบเป็นมันเงา
	2= ขนสั้นและหนาช่วยกันน้ำ
	3= ขนแข็งคล้ายเส้นลวด
	4= ขนคล้ายเส้นไหม
	5= ขนยาว
	6= ขนหยิกไม่ผลัดขน
	 */
	private Map<String, String> initializeCoats()
	{
		TreeMap<String, String> coats = new TreeMap<>();
		coats.put(Dog.COAT_1_SHORT_FLAT_POLISHED, getText("ขนสั้นและเรียบเป็นมันเงา"));
		coats.put(Dog.COAT_2_SHORT_THICK_WATERPROOF, getText("ขนสั้นและหนาช่วยกันน้ำ"));
		coats.put(Dog.COAT_3_WIRE_BROKEN, getText("ขนแข็งคล้ายเส้นลวด"));
		coats.put(Dog.COAT_4_SILK, getText("ขนคล้ายเส้นไหม"));
		coats.put(Dog.COAT_5_LONG, getText("ขนยาว"));
		coats.put(Dog.COAT_6_CURLY_NO_SHED, getText("ขนหยิกไม่ผลัดขน"));
		
		return coats;
	}
	
	private Map<String, String> initializeFlag()
	{
		TreeMap<String, String> flag = new TreeMap<>();
		flag.put(Dog.YES, getText("ใช่"));
		flag.put(Dog.NO, getText("ไม่ใช่"));
		
		return flag;
	}
	
	private Map<String, String> initializeWeek( int noOfWeek )
	{
		TreeMap<String, String> weeks = new TreeMap<>();
		for ( int i = 1; i <= noOfWeek; i++ )
		{
			weeks.put(Integer.toString(i), Integer.toString(i));
		}
		return weeks;
	}
	
	/**
	 * 	1= น้อยกว่า 1 ชั่วโมงต่อวัน
		2= ระหว่าง 1 - 3 ชั่วโมงต่อวัน
		3= 3 ชั่วโมงขึ้นไป
	 * @return
	 */
	private Map<String, String> initializeActivityLevel()
	{
		TreeMap<String, String> activityLevel = new TreeMap<>();
		activityLevel.put(Dog.ACTIVITY_LEVEL_1_LESS_THAN_1_HOUR, getText("น้อยกว่า 1 ชั่วโมงต่อวัน"));
		activityLevel.put(Dog.ACTIVITY_LEVEL_2_BETWEEN_1_TO_3_HOUR, getText("ระหว่าง 1 - 3 ชั่วโมงต่อวัน"));
		activityLevel.put(Dog.ACTIVITY_LEVEL_3_MORE_THAN_3_HOUR, getText("3 ชั่วโมงขึ้นไป"));
		
		return activityLevel;
	}
	
	@Override
	public void setSession(Map<String, Object> userSession)
	{
		this.userSession = userSession;
	}

	public Dog getDogForm()
	{
		return dogForm;
	}

	public void setDogForm(Dog dogForm)
	{
		this.dogForm = dogForm;
	}

	public Dog getDog()
	{
		return dog;
	}

	public void setDog(Dog dog)
	{
		this.dog = dog;
	}

	public Member getMember()
	{
		return member;
	}

	public void setMember(Member member)
	{
		this.member = member;
	}

	public Map<String, String> getBreedNameList()
	{
		return breedNameList;
	}

	public void setBreedNameList(Map<String, String> breedNameList)
	{
		this.breedNameList = breedNameList;
	}

	public Map<String, String> getGenders()
	{
		return genders;
	}

	public void setGenders(Map<String, String> genders)
	{
		this.genders = genders;
	}

	public Map<String, String> getMonths()
	{
		return months;
	}

	public void setMonths(Map<String, String> months)
	{
		this.months = months;
	}

	public Map<String, String> getYears()
	{
		return years;
	}

	public void setYears(Map<String, String> years)
	{
		this.years = years;
	}

	public Map<String, String> getCoats()
	{
		return coats;
	}

	public void setCoats(Map<String, String> coats)
	{
		this.coats = coats;
	}

	public Map<String, String> getManeFlag()
	{
		return maneFlag;
	}

	public void setManeFlag(Map<String, String> maneFlag)
	{
		this.maneFlag = maneFlag;
	}

	public Map<String, String> getPregnantWeekList()
	{
		return pregnantWeekList;
	}

	public void setPregnantWeekList(Map<String, String> pregnantWeekList)
	{
		this.pregnantWeekList = pregnantWeekList;
	}

	public Map<String, String> getActivityLevelList()
	{
		return activityLevelList;
	}

	public void setActivityLevelList(Map<String, String> activityLevelList)
	{
		this.activityLevelList = activityLevelList;
	}

	public Map<String, String> getAllergyToBeefFlag()
	{
		return allergyToBeefFlag;
	}

	public void setAllergyToBeefFlag(Map<String, String> allergyToBeefFlag)
	{
		this.allergyToBeefFlag = allergyToBeefFlag;
	}

	public Map<String, String> getAllergyToPorkFlag()
	{
		return allergyToPorkFlag;
	}

	public void setAllergyToPorkFlag(Map<String, String> allergyToPorkFlag)
	{
		this.allergyToPorkFlag = allergyToPorkFlag;
	}

	public Map<String, String> getAllergyToChickenFlag()
	{
		return allergyToChickenFlag;
	}

	public void setAllergyToChickenFlag(Map<String, String> allergyToChickenFlag)
	{
		this.allergyToChickenFlag = allergyToChickenFlag;
	}

	public Map<String, String> getAllergyToFishFlag()
	{
		return allergyToFishFlag;
	}

	public void setAllergyToFishFlag(Map<String, String> allergyToFishFlag)
	{
		this.allergyToFishFlag = allergyToFishFlag;
	}

	public Map<String, String> getAllergyToMilkFlag()
	{
		return allergyToMilkFlag;
	}

	public void setAllergyToMilkFlag(Map<String, String> allergyToMilkFlag)
	{
		this.allergyToMilkFlag = allergyToMilkFlag;
	}

	public Map<String, String> getAllergyToWheyFlag()
	{
		return allergyToWheyFlag;
	}

	public void setAllergyToWheyFlag(Map<String, String> allergyToWheyFlag)
	{
		this.allergyToWheyFlag = allergyToWheyFlag;
	}

	public Map<String, String> getAllergyToEggFlag()
	{
		return allergyToEggFlag;
	}

	public void setAllergyToEggFlag(Map<String, String> allergyToEggFlag)
	{
		this.allergyToEggFlag = allergyToEggFlag;
	}

	public Map<String, String> getAllergyToSoybeanFlag()
	{
		return allergyToSoybeanFlag;
	}

	public void setAllergyToSoybeanFlag(Map<String, String> allergyToSoybeanFlag)
	{
		this.allergyToSoybeanFlag = allergyToSoybeanFlag;
	}

	public Map<String, String> getAllergyToCornFlag()
	{
		return allergyToCornFlag;
	}

	public void setAllergyToCornFlag(Map<String, String> allergyToCornFlag)
	{
		this.allergyToCornFlag = allergyToCornFlag;
	}

	public Map<String, String> getDogNameList()
	{
		return dogNameList;
	}

	public void setDogNameList(Map<String, String> dogNameList)
	{
		this.dogNameList = dogNameList;
	}

	public Map<String, String> getLactateWeekList()
	{
		return lactateWeekList;
	}

	public void setLactateWeekList(Map<String, String> lactateWeekList)
	{
		this.lactateWeekList = lactateWeekList;
	}

}
